﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Entities;
using GameEngine.EntityComponents;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameEngine.Camera;

namespace PvT.Entities.HUD
{
    public class HealthBarHudEntity : AbstractHudEntity
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="HealthBarHudEntity" /> class.
        /// </summary>
        /// <param name="components">The components.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isVisible">if set to <c>true</c> [is visible].</param>
        /// <param name="position">The position.</param>
        /// <param name="game">The game.</param>
        public HealthBarHudEntity(List<IEntityComponent> components, bool isActive, bool isVisible, Vector2 position, Game game) :
            base(components, isActive, isVisible, position, game)
        { }

        /// <summary>
        /// Loads the content.
        /// </summary>
        public override void LoadContent()
        {
            base.texture = game.Content.Load<Texture2D>("Hud/health-bar");
            sourceRectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
        }

        /// <summary>
        /// Updates the HealthBarHudEntity
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public override void Update(GameTime gameTime)
        {
            Camera2D camera = (Camera2D)game.Services.GetService(typeof(Camera2D));
            this.position = new Vector2(camera.Pos.X, camera.Pos.Y);

            UpdateComponents(gameTime);
        }

        /// <summary>
        /// Draws the HealtBadHudEntity
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        /// <param name="gameTime">The game time.</param>
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Camera2D camera = (Camera2D)game.Services.GetService(typeof(Camera2D));

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.GetTransformation());

            // Set health percentage
            float healthPercentage = 0f;

            TerminateableComponent enemyTerminateable = (TerminateableComponent) player.GetComponent(typeof(TerminateableComponent));
            if (enemyTerminateable != null)
                healthPercentage = enemyTerminateable.GetHealthPercentage();

            float visibleWidth = (float)texture.Width * (healthPercentage);
            float red = (healthPercentage < 0.5 ? 1 : 1 - (2 * healthPercentage - 1));
            float green = (healthPercentage > 0.5 ? 1 : (2 * healthPercentage));
            Color healthColor = new Color(red, green, 0);

            Rectangle healthRectangle = new Rectangle((int)camera.Pos.X - (game.GraphicsDevice.Viewport.Width / 4) + 10,
                                            (int)camera.Pos.Y - (game.GraphicsDevice.Viewport.Height / 4) + 10,
                                            (int)visibleWidth * 2, texture.Height*2);

            spriteBatch.Draw(texture, healthRectangle, healthColor);
            //spriteBatch.Draw(texture, healthRectangle, Color.Gray);
            
            spriteBatch.End();
        }
    }
}